package com.wsoft.websocket.util;

import com.rabbitmq.client.Channel;
import com.wsoft.mq.constant.ImConstant;
import com.wsoft.mq.constant.SmsConstant;
import com.wsoft.mq.vo.MessageVO;
import com.wsoft.websocket.config.WebSocketHandler;
import com.wsoft.websocket.entity.RabbitMission;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;

/**
 * @Author Zhang gukai
 * @Date 2025/1/9 8:45
 */
    @ServerEndpoint(value = "/ws")
    @Component
    public class WebSocketServer {

    @Resource
    private WebSocketHandler webSocketHandler;
        /**
         * 参数1:Message 可以获得消息的内容字节 还可以获得消息的其他属性
         * 参数2:可以写确定接受的参数类型比如User
         * 参数3:Channel 通道
         * 通过这个参数可以拒绝消息
         * 让rabbitmq再发给别的消费者
         * <p>
         * 使用@RabbitListener 可以绑定交换机  路由键 管道
         */
        @RabbitListener(queues = SmsConstant.SMS_TO_USER_QUEUE,ackMode="MANUAL")
        @RabbitHandler
        public void receiveUserMessage(@Payload MessageVO message, @Headers Map<String, Object> headers, Channel channel) throws IOException {

            String[] clientSplit = message.getClientType().split(",");
            for (String client : clientSplit){
                if ("web".equals(client)){
                    for (String userId : message.getMemberIdList()){
                        //调用消息方法将数据船体给他
                        webSocketHandler.sendMessageToWeb(userId, new TextMessage(message.getData().toString()));
                        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
                        //手动接受并告诉rabbitmq消息已经接受了  deliverTag记录接受消息 false不批量接受
                        channel.basicAck(deliveryTag, false);
                    }
                }
                if ("app".equals(client)){
                    for (String userId : message.getMemberIdList()){
                        //调用消息方法将数据船体给他
                        webSocketHandler.sendMessageToApp(userId, new TextMessage(message.getData().toString()));
                        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
                        //手动接受并告诉rabbitmq消息已经接受了  deliverTag记录接受消息 false不批量接受
                        channel.basicAck(deliveryTag, false);
                    }
                }
                if ("xcx".equals(client)){
                    for (String userId : message.getMemberIdList()){
                        //调用消息方法将数据船体给他
                        webSocketHandler.sendMessageToXcx(userId, new TextMessage(message.getData().toString()));
                        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
                        //手动接受并告诉rabbitmq消息已经接受了  deliverTag记录接受消息 false不批量接受
                        channel.basicAck(deliveryTag, false);
                    }
                }
            }
        }
}


